<template>
  <div class="app-container">
    商品信息
    <el-form ref="form" :model="form" label-width="80px">
      <el-form-item label="商品名称">
        <el-input v-model="form.title"></el-input>
      </el-form-item>
      <el-form-item label="商品卖点">
        <el-input v-model="form.sellPoint"></el-input>
      </el-form-item>
      <el-form-item label="商品价格">
        <el-input v-model="form.price"></el-input>
      </el-form-item>
      <el-form-item label="库存数量">
        <el-input v-model="form.num"></el-input>
      </el-form-item>

      <el-form-item label="所属类目">
        <el-select v-model="form.cid" placeholder="请选择商品所属类目">
          <el-option label="饮料" value=1></el-option>
          <el-option label="零食" value=2></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="商品状态">
        <el-select v-model="form.status" placeholder="请选择商品状态">
          <el-option label="正常" value="1"></el-option>
          <el-option label="下架" value="2"></el-option>
          <el-option label="删除" value="3"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button :disabled="saveBtnDisabled" type="primary" @click="saveOrUpdate">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<style>
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
</style>

<script>
import item from "@/api/item";
export default {
  data() {
    return {
      form: {
        title: "",
        sellPoint: "",
        price: 0,
        num: 0,
        cid: null,
        status: null,
      },
      saveBtnDisabled: false,
    };
  },
  created() {
    //页面渲染之前执行
    this.init();
  },
  watch: {
    //监听
    $route(to, from) {
      //路由变化方式，路由发生变化，方法就会执行
      this.init();
    },
  },
  methods: {
    init() {
      //判断路径有id值,做修改
      if (this.$route.params && this.$route.params.id) {
        //从路径获取id值
        const id = this.$route.params.id;
        //调用根据id查询的方法
        this.getInfo(id);
      } else {
        //路径没有id值，做添加
        //清空表单
        this.teacher = {};
      }
    },
    //根据商品id查询的方法
    getInfo(id) {
      item.getItemInfo(id).then((response) => {
        this.form = response.data.item;
      });
    },
    saveOrUpdate() {
      //判断修改还是添加
      //根据商品是否有id
      if (!this.form.id) {
        //添加
        this.saveItem();
      } else {
        //修改
        this.updateItem();
      }
    },
    //修改讲师的方法
    updateItem() {
      item.updateItemInfo(this.form).then((response) => {
        //提示信息
        this.$message({
          type: "success",
          message: "修改成功!",
        });
        //回到列表页面 路由跳转
        this.$router.push({ path: "/item/list" });
      });
    },
    //添加讲师的方法
    saveItem() {
      item.addItem(this.form).then((response) => {
        //添加成功
        //提示信息
        this.$message({
          type: "success",
          message: "添加成功!",
        });
        //回到列表页面 路由跳转
        //vue-element-admin\src\views\edu\teacher\list.vue
        this.$router.push({ path: "/item/list" });
      });
    },
  },
};
</script>